package com.xone.xapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SubscribeItemUserActivity extends BasicActivity {
	protected final String TAG = SubscribeItemUserActivity.class.getSimpleName();
    protected LayoutInflater mInflater;
    protected String id;
    protected LinearLayout view;
	@Override
	public String getHeaderTitle() {
		return "订阅详情";
	}
	@Override
	public int getContentView() {
		return R.layout.subscribe_item_user;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		Bundle bundle = this.getIntent().getExtras();
		id = (null == bundle) ? "" : bundle.getString("id");
		super.onCreate(savedInstanceState);
		if (!ContextUtils.isOnline(this)) {
			return;
		}
		if (null == id) {
			Toast.makeText(this, "缺少请求参数", Toast.LENGTH_SHORT).show();
			this.finish();
			return;
		}
		view = (LinearLayout)findViewById(R.id.subscribe_item_details_zone);
		this.mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	/**
	 * 程序重新加载时的的请求
	 */
	protected void onResume() {
		super.onResume();
		showProgress();
		new InitDataTask(this, view).execute(id);
	}
	
	public class InitDataTask extends AsyncTask<String, Integer, JSONObject> {
		protected Context context;
		protected LinearLayout view;
		public InitDataTask(Context context, LinearLayout view) {
			this.context = context;
			this.view = view;
		}
		@Override
		protected JSONObject doInBackground(final String... params) {
			if (isCancelled()) {
				return null;
			}
			List<NameValuePair> reqparams = new ArrayList<NameValuePair>();
			reqparams.add(new BasicNameValuePair("subscribe.id", params[0]));
			return HttpUtils.httpGet(Constants.APP_URL + "/subscribe/itemJson.html", reqparams, 
					new HttpUtils.onGetSuccessListener<JSONObject>() {
						@Override
						public JSONObject onSuccess(int status, String result) {
							if (HttpUtils.SC_OK == status) {
								JSONObject jsonObject = JSONUtils.getJSONObjectFromString(result);
								JSONObject subscribe = JSONUtils.getJSONObjectForKey(jsonObject, "subscribe");
								return subscribe;
							} else {
								Toast.makeText(context, "Status Code:" + status, Toast.LENGTH_SHORT).show();
							}
							return null;
						}
					}, context);
		}
		@Override
		protected void onPostExecute(JSONObject result) {
			hideProgress();
			super.onPostExecute(result);
			if (null == result) {
				return;
			}
			JSONObject subscribe = result;
			ViewTag viewTag = new ViewTag();
			//设置数据体
			viewTag.setId(JSONUtils.getStringFromJSONObject(subscribe, "id"));
			viewTag.setCredit(JSONUtils.getStringFromJSONObject(subscribe, "credit"));
			viewTag.setSaleType(JSONUtils.getStringFromJSONObject(subscribe, "saleType"));
			viewTag.setMarketarea(JSONUtils.getStringFromJSONObject(subscribe, "marketarea"));
			viewTag.setProductNameKey(JSONUtils.getStringFromJSONObject(subscribe, "productNameKey"));
			//更新按钮的操作
			View editView = view.findViewById(R.id.subscribe_update);
			editView.setTag(viewTag);
			editView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {//编辑订阅
					ViewTag viewTag = (ViewTag)v.getTag();
					viewTag.setType("0");
					linkAction(viewTag);
				}
			});
			View itemsView = view.findViewById(R.id.subscribe_items);
			itemsView.setTag(viewTag);
			itemsView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {//产品订阅列表
					ViewTag viewTag = (ViewTag)v.getTag();
					viewTag.setType("1");
					linkAction(viewTag);
				}
			});
			int childCount = view.getChildCount();
			for (int i = 0 ; i < childCount; i++) {
				View v = view.getChildAt(i);
				String tag = (String)v.getTag();
				if (v instanceof LinearLayout && null != tag) {
					String value = JSONUtils.getStringFromJSONObject(subscribe, tag);
					if (null != value) {
						TextView tv = (TextView)view.findViewById(context.getResources().getIdentifier(tag, "id", context.getPackageName()));
						if (null != tv) {
							if ("lastUpdated".equals(tag) || "dateCreated".equals(tag)) {
								tv.setText(StringUtils.formatStringDate(value));
							} else {
								tv.setText(value);
							}
						}
					}
				}
			}
		}
	}
	
	/**
	 * 点击更新按钮的事件
	 * @param tag
	 */
	protected void linkAction(final ViewTag tag) {
		String type = tag.getType();
		Intent intent = null;
		if ("0".equals(type)) {//编辑信息
			intent = new Intent(SubscribeItemUserActivity.this, SubscribeUpdateActivity.class);
		} else if ("1".equals(type)) {//查看订阅列表
			intent = new Intent(SubscribeItemUserActivity.this, SubscribeMatcherListAcitvity.class);
			intent.putExtra("name", tag.getProductNameKey());
			if (StringUtils.isNotBlank(tag.getCredit())) {
				intent.putExtra("credit", tag.getCredit());
			}
			if (StringUtils.isNotBlank(tag.getSaleType())) {
				intent.putExtra("saleType", tag.getSaleType());
			}
			if (StringUtils.isNotBlank(tag.getMarketarea())) {
				intent.putExtra("area", tag.getMarketarea());
			}
		}
		intent.putExtra("id", tag.getId());
		startActivity(intent);
	}
	
	protected class ViewTag {
		protected String id;
		protected String type;
		protected String marketarea;
		protected String productNameKey;
		protected String saleType;
		protected String credit;
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getType() {
			return type;
		}
		public void setType(String type) {
			this.type = type;
		}
		public String getMarketarea() {
			return marketarea;
		}
		public void setMarketarea(String marketarea) {
			this.marketarea = marketarea;
		}
		public String getProductNameKey() {
			return productNameKey;
		}
		public void setProductNameKey(String productNameKey) {
			this.productNameKey = productNameKey;
		}
		public String getSaleType() {
			return saleType;
		}
		public void setSaleType(String saleType) {
			this.saleType = saleType;
		}
		public String getCredit() {
			return credit;
		}
		public void setCredit(String credit) {
			this.credit = credit;
		}
	}
}
